package com.kly.msg.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @ClassName: LittleOrangeService
 * @description:
 * @author: yuwn
 * @create: 2023-11-28 20:50
 */
public class LittleOrangeService {

    private static final Logger LOGGER = LoggerFactory.getLogger(LittleOrangeService.class);


    public static String post(String url, String requestBody,String sign,String dateTime,String apiKey) {


        String[] command = {"curl", "--location", "--request", "POST", url,
                "--header", "accept: application/json",
                "--header","Sign:"+sign,
                "--header","Api-Key:"+apiKey,
                "--header","Timestamp:"+dateTime,
                "--header", "content-type: application/json; charset=utf-8",
                "--data-raw", requestBody};
        return execCurl(command);
    }


    public static String execCurl(String[] cmds) {
        ProcessBuilder process = new ProcessBuilder(cmds);
        Process p;
        try {
            p = process.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            StringBuilder builder = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
//                builder.append(System.getProperty("line.separator"));
            }
            return builder.toString();

        } catch (IOException e) {
            System.out.print("error");
            e.printStackTrace();
        }
        return null;
    }
}
